package base;

import java.util.Comparator;


public class ShipSilhouetteComparator implements Comparator<ShipSilhouette> {

	public ShipSilhouetteComparator(double startAngle) {
		this.startAngle = startAngle;
	}
	
	private double startAngle = 0;
	
	/*
	 * Compares its two arguments for order. Returns a negative integer, zero, or a positive integer 
	 * as the first argument is less than, equal to, or greater than the second.
	 * 
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	@Override
	public int compare(ShipSilhouette first, ShipSilhouette second) {
		boolean firstContains = first.fromTurret.contains(startAngle);
		boolean secondContains = second.fromTurret.contains(startAngle);
		if (firstContains && !secondContains )
		{
			return -1;
		}
		if (!firstContains && secondContains)
		{
			return 1;
		}
		
		// Of both of the arcs contain the starting angle, then whichever begins first is first.
		// Also, if neither of the arcs contain the starting angle, then whichever begins first is first.
		// Thus the rest is handled the same, regardless if both or neither of the arcs contain the starting angle.
		double diffFirst = Geometry.ensurePositive(first.fromTurret.beginAngle - startAngle);
		double diffSecond = Geometry.ensurePositive(second.fromTurret.beginAngle - startAngle);
		
		if (diffFirst < diffSecond)
		{
			return -1;
		}
		else if (diffFirst > diffSecond)
		{
			return 1;
		}
		return 0;
	}

}
